﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JsonServer.FYPDataSetTableAdapters;

namespace JsonServer
{
    public partial class WebFormMonth : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        int datacount = 0;
        protected void ButtonCalculateMonthPower_Click(object sender, EventArgs e)
        {
            string year = this.TextBoxOfMonthPageYear.Text;
            string month = this.TextBoxOfMonthPageMonth.Text;
            double totalOneMonthPower = Convert.ToDouble(GetOneMonthPower(year, month)) / 3600;
            string formatedTotalPower = totalOneMonthPower.ToString("#.###");
            this.LabelMonthPower.Text = "Total one month power is " + formatedTotalPower + "kWh.";
        }

        public string GetOneMonthPower(string year, string month)
        {
            // lookup person with the requested id 
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetDataByMonth(year, month);
            double MonthPower = 0;

            foreach (var item in powerTable)
            {
                double eachPower = Convert.ToDouble(item.Power);
                MonthPower += eachPower;
                datacount++;
            }
            string sMonthPower = MonthPower.ToString("#.###0");
            return sMonthPower;
        }
    }
}